package com.cswnode.demo.controller;


import com.cswnode.demo.dao.EnterpriseDao;
import com.cswnode.demo.entity.Enterprise;
import com.cswnode.demo.entity.Page;
import com.cswnode.demo.service.EnterpriseService;
import com.cswnode.demo.util.ResponseVo;
import com.cswnode.demo.vo.EnterpriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author csw
 * @create 2021/3/13 20:33
 */
@Api(tags = "企业管理模块")
@RequestMapping("/enterprise")
@RestController
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private EnterpriseDao enterpriseDao;

    @ApiOperation(value = "添加企业")
    @PostMapping("/add")
    public ResponseVo add(@RequestBody Enterprise enterprise){
        if (ObjectUtils.isEmpty(enterprise)) {
            return ResponseVo.error("企业信息不能为空!");
        }
        enterpriseService.insert(enterprise);
        return ResponseVo.success("添加成功！");
    }

    @ApiOperation(value = "获得所有企业")
    @GetMapping("/query")
    public ResponseVo query(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10")Integer limit,
                            String entName, String grade)
    {
        Map<String,Object> result = new HashMap<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setLimit(limit);
        page.setRows(enterpriseService.selectNum(entName,grade));
        List<EnterpriseVo> enterpriseVos = enterpriseService.queryAllByVo(page.getOffset(), page.getLimit(), entName, grade);
        result.put("page",page);
        result.put("enterpriseVos",enterpriseVos);
        return ResponseVo.success(result);
    }
    @ApiOperation(value = "获得所有企业选项")
    @GetMapping("/getOptions")
    public ResponseVo getOptions(){
        List<Enterprise> query = enterpriseDao.query();
        return ResponseVo.success(query);
    }

    @ApiOperation(value = "修改企业")
    @PostMapping("/update")
    public ResponseVo update(@RequestBody Enterprise enterprise){
        String update = enterpriseService.update(enterprise);
        return ResponseVo.success(update);
    }

    @ApiOperation(value = "删除专业")
    @GetMapping("/delete/{entId}")
    public ResponseVo delete (@PathVariable("entId")Long entId){
        if (entId == null){
            return ResponseVo.error("删除失败！");
        }
        boolean result = enterpriseService.deleteById(entId);
        if (!result){
            return ResponseVo.error("删除失败");
        }
        return ResponseVo.success("删除成功");
    }
}
